import mongoose, { Document, Schema } from 'mongoose';
import { OilPriceData } from '@/types/oilPrice';

export interface IOilPriceDocument
  extends Omit<OilPriceData, '_id'>,
    Document {}

const oilPriceSchema = new Schema<IOilPriceDocument>(
  {
    province: {
      type: String,
      required: [true, '省份是必需的'],
      trim: true,
      index: true,
    },
    price92: {
      type: Number,
      required: [true, '92号汽油价格是必需的'],
      min: [0, '价格不能为负数'],
    },
    price95: {
      type: Number,
      required: [true, '95号汽油价格是必需的'],
      min: [0, '价格不能为负数'],
    },
    price98: {
      type: Number,
      required: [true, '98号汽油价格是必需的'],
      min: [0, '价格不能为负数'],
    },
    price0: {
      type: Number,
      required: [true, '0号柴油价格是必需的'],
      min: [0, '价格不能为负数'],
    },
    updateTime: {
      type: Date,
      required: [true, '更新时间是必需的'],
      default: Date.now,
      index: true,
    },
    source: {
      type: String,
      required: [true, '数据来源是必需的'],
      trim: true,
      default: 'eastmoney',
    },
  },
  {
    timestamps: true,
  }
);

// 索引
oilPriceSchema.index({ province: 1, updateTime: -1 });
oilPriceSchema.index({ updateTime: -1 });
oilPriceSchema.index({ source: 1, updateTime: -1 });

// 复合索引
oilPriceSchema.index({ province: 1, source: 1, updateTime: -1 });

export const OilPrice = mongoose.model<IOilPriceDocument>(
  'OilPrice',
  oilPriceSchema
);
